﻿using CefSharp;
using KeZhou.Models.Instances;
using KeZhou.Views.Components.Controls;
using Models.Wishire;
using Models.Wishire.entity;
using Models.Wishire.Operator;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace KeZhou.ViewModels
{
    internal class PlanViewModel
    {
        private static PlanViewModel instance;
        public solve solve = new solve();
        public static PlanViewModel Instance
        {
            get
            {
                return instance ?? (instance = new PlanViewModel());
            }
        }

        PlanViewModel()
        {
            
        }

        //初始化时获取全部的计划
        public void initGetPlanAndAnswer()
        {
            //TODO@wxy
            //获取用户所有有answer的plan
            var plan = solve.get_allcity((int)PlanOnBuilding.Instance.accountId); 
            List<PageFourPlan> planList = new List<PageFourPlan>();
            for (int i=0;i<plan.Count;i++) {
                planList.Add(
                    new PageFourPlan(
                        plan[i].GetPlanId(),
                        plan[i].GetPlanName(),
                        plan[i].GetCityName(),
                        plan[i].GetStartTime(),
                        plan[i].GetEndTime(),
                        plan[i].GethotelName(),
                        plan[i].GethotelLat(),
                        plan[i].GethotelLng(),
                        plan[i].GetEmpty()
                    ));
            }
            /*List<PageFourPlan> planList = new List<PageFourPlan>
            {
                new PageFourPlan(1,"武汉一日游","武汉","2023-12-10-0","2023-12-10-2","如家酒店（武汉大学店）",0,0,"default"),
                new PageFourPlan(3,"武汉两日游","武汉","2023-12-10-0","2023-12-11-2","如家酒店（武汉大学店）",0,0,"default"),
                new PageFourPlan(4,"广州一日游","广州","2023-12-10-0","2023-12-10-1","如家酒店(广州大学店)",0,0,"default"),
            };*/
            //List<int> planIds = solve.find_all_generated_planId((int)PlanOnBuilding.Instance.accountId);
            List<PageFiveAnswerStru> allAnswers = new List<PageFiveAnswerStru>();
            for (int i = 0; i < plan.Count; i++)
            {
                PageFiveAnswerStru temp = new PageFiveAnswerStru();
                temp.planId = plan[i].GetPlanId();
                opdataanswer opans = new opdataanswer();
                var answers = opans.answerFind_planId(plan[i].GetPlanId());
                List<PageFiveAnswer> answerlist = new List<PageFiveAnswer>();
                for(int j=0;j< answers.Count; j++)
                {
                    answerlist.Add(
                        new PageFiveAnswer(
                            answers[j].GetAnswerId(), answers[j].GetPlanId(),
                            answers[j].GetName(), answers[j].GetIfDeleted(), answers[j].GetIndex(), answers[j].GetLat(), answers[j].GetLng()
                            ));
                }
                allAnswers.Add(new PageFiveAnswerStru(plan[i].GetPlanId(), answerlist));
            }
            /*List<PageFiveAnswerStru> allAnswers = new List<PageFiveAnswerStru>
            {
                new PageFiveAnswerStru(1, new List<PageFiveAnswer>
                {
                    new PageFiveAnswer(1,1,"武汉大学",0,1,0,0),
                    new PageFiveAnswer(2,1,"银泰",0,2,0,0),
                    new PageFiveAnswer(3,1,"不想去删掉",1,3,0,0),
                    new PageFiveAnswer(4,1,"群光",0,4,0,0),
                }),
                new PageFiveAnswerStru(3, new List<PageFiveAnswer>
                {
                    new PageFiveAnswer(5,3,"武汉大学",0,1,0,0),
                    new PageFiveAnswer(6,3,"不想去删掉",1,2,0,0),
                    new PageFiveAnswer(7,3,"不想去删掉",1,3,0,0),
                    new PageFiveAnswer(8,3,"群光",0,4,0,0),
                }),
                new PageFiveAnswerStru(4, new List<PageFiveAnswer>
                {
                    new PageFiveAnswer(9,4,"不想去删掉",1,1,0,0),
                    new PageFiveAnswer(10,4,"不想去删掉",1,2,0,0),
                }),
            };*/
            //end
            var json = JsonConvert.SerializeObject(new { planList = planList, allAnswers = allAnswers });
            MainFivePlan.Instance.Browser.ExecuteScriptAsyncWhenPageLoaded($"document.initPlanAndAnswer({json});");
        }

        public void exchangeTwoAnswers(PageFiveTmpExchange exchangeArgs)
        {
            //TODO@wxy
            //交换两个answer的index
            int planId = exchangeArgs.planId;
            int index1 = exchangeArgs.index1;
            int index2 = exchangeArgs.index2;
            solve.generate_swap(planId, index1, index2);
            //end
        }

        public void dontGo(int answerId)
        {
            //TODO@wxy
            //将answer的ifDeleted置为1
            opdataanswer op = new opdataanswer();
            answer ans = op.answerFind_answerId(answerId);
            ans.SetIfDeleted(1);
            op.answerUpdate(ans);
            //end
        }

        //新计划更新
        public void jumpFromInitPlan(PageFourPlan currentPlan, List<PageFiveAnswer> currentPlanAnswerlist)
        {
            /* var json = JsonConvert.SerializeObject(new { newPlan = currentPlan, newPlanAnswerList = currentPlanAnswerlist });
             MainFivePlan.Instance.Browser.ExecuteScriptAsyncWhenPageLoaded($"document.newPlanAnswerSet({json});");*/
            
        }


    }

    class PageFiveAnswerStru
    {
        public int planId { get; set; }

        public List<PageFiveAnswer> answerList { get; set; }

        public PageFiveAnswerStru() { }

        public PageFiveAnswerStru(int planId, List<PageFiveAnswer> answerList)
        {
            this.planId = planId;
            this.answerList = answerList;
        }
        
    }

    class PageFiveTmpExchange
    {
        public int planId { set; get; }
        public int index1 { set; get; }
        public int index2 { set; get; }
    }
}
